package io.filecopy;

import java.io.*;

public class Demo03DirectaryCopy {
    public static void main(String[] args) {
        File file = new File("F://Test");
        File target = new File("F://TestCopy");
        copy(file, target);
    }

    public static void copy(File file, File target) {
        File[] files = file.listFiles();
        for (File f : files) {
            File des = new File(target, f.getName());
            if (f.isDirectory()) {
                des.mkdirs();
                copy(f, des);
            } else {
                write(f, des);
            }
        }
    }

    public static void write(File src, File des) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(src);
            fos = new FileOutputStream(des);
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fis.read(bytes)) != -1) {
                fos.write(bytes);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
